package com.sample.servicefragment;

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

public class CustomLifecycleService extends LifecycleService implements
        ViewModelStoreOwner {

    private ViewModelStore mViewModelStore = null;

    private static LayoutInflater mLayoutInflater = null;


    public CustomLifecycleService() {
        getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source,
                                       @NonNull Lifecycle.Event event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    getViewModelStore().clear();
                }
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(@NonNull Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        if (getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the "
                    + "Application instance. You can't request ViewModel before onCreate call.");
        }
        if (mViewModelStore == null) {
            mViewModelStore = new ViewModelStore();
        }
        return mViewModelStore;
    }

    public class DummyFragmentManager {

        public DummyFragmentTransaction beginTransaction() {
            return new DummyFragmentTransaction();
        }

    }

    public class DummyFragmentTransaction {
        @NonNull
        public DummyFragmentTransaction add(@NonNull DummyServiceFragment fragment, @Nullable String tag)  {
            if(!fragment.isAdded()) {
                fragment.setContext(CustomLifecycleService.this);
                fragment.onCreate();
//            fragment.onCreateView();
                fragment.addView();
                fragment.setAdded(true);
            }
            return this;
        }

        @NonNull
        public DummyFragmentTransaction remove(@NonNull DummyServiceFragment fragment) {
            if(fragment.isAdded()) {
                fragment.removeView();
                fragment.setAdded(false);
            }
            return this;
        }

        public int commit() {
            return 0;
        }

    }

    public LayoutInflater getLayoutInflater() {
        if(null == mLayoutInflater) {
            mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (mLayoutInflater == null) {
                throw new AssertionError("LayoutInflater not found.");
            }
        }
        return mLayoutInflater;
    }

    private DummyFragmentManager mDummyFragmentManager = null;

    public DummyFragmentManager getSupportFragmentManager() {
        if(null == mDummyFragmentManager) {
            mDummyFragmentManager = new DummyFragmentManager();
        }
        return mDummyFragmentManager;
    }

}
